package com.avecsys.quotemanager.mbeans.supplier;

import java.io.Serializable;
import java.util.Map;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.avecsys.quotemanager.dao.common.EnumTransactionStatus;
import com.avecsys.quotemanager.exception.BusinessException;
import com.avecsys.quotemanager.mbeans.core.BaseMBean;
import com.avecsys.quotemanager.pojos.SupplierMasterPojo;
import com.avecsys.quotemanager.pojos.UserMasterPojo;
import com.avecsys.quotemanager.service.supplier.SupplierMasterService;
import com.avecsys.quotemanager.service.user.UserMasterService;

public class SupplierUsersMBean extends BaseMBean implements Serializable {

	private final Log log = LogFactory.getLog(SupplierUsersMBean.class);

	private SupplierMasterService supplierMasterService;

	private UserMasterService userMasterService;

	private UserMasterPojo userMasterPojo = new UserMasterPojo();

	private String selectedSupplierId;

	private String selectedSupplierCompanyName;

	private String confirmPassword;

	public String actionCancel() {
		try {
			FacesContext faceContext = FacesContext.getCurrentInstance();
			Map<String, String> paramValues = faceContext.getExternalContext()
					.getRequestParameterMap();
			String selectedSupplierId = paramValues.get("selectedSupplierId");
			SupplierMasterPojo selectedSupplierMasterPojo = supplierMasterService
					.getSupplierMasterById(new Long(selectedSupplierId));
			SupplierUsersListMBean supplierUsersMBean = (SupplierUsersListMBean) faceContext
					.getApplication().evaluateExpressionGet(faceContext,
							"#{supplierUsersListMBean}",
							SupplierUsersListMBean.class);
			supplierUsersMBean
					.loadAllUsersBySupplier(selectedSupplierMasterPojo);
		} catch (BusinessException e) {
			log.error(e);
		}
		return "openListUsersForSupplier";
	}

	public String actionSaveSupplierUser() {
		try {
			log.info("Started Saving Supplier User Information into database");
			SupplierMasterPojo selectedSupplierMasterPojo = supplierMasterService
					.getSupplierMasterById(new Long(getSelectedSupplierId()));
			userMasterPojo.setSupplierMasterPojo(selectedSupplierMasterPojo);
			Integer transactionStatus = userMasterService
					.saveSupplierUser(userMasterPojo);
			log.info("Completed saving Supplier User Information into database sucessfully");
			if (transactionStatus.intValue() == EnumTransactionStatus.SUCESS.ordinal()) {
				FacesContext faceContext = FacesContext.getCurrentInstance();
				SupplierUsersListMBean supplierUsersMBean = (SupplierUsersListMBean) faceContext
						.getApplication().evaluateExpressionGet(faceContext,
								"#{supplierUsersListMBean}",
								SupplierUsersListMBean.class);
				supplierUsersMBean
						.loadAllUsersBySupplier(selectedSupplierMasterPojo);
			} else {
			}
		} catch (BusinessException e) {
			log.error(e);
		}
		return "openListUsersForSupplier";
	}

	/**
	 * @return the confirmPassword
	 */
	public String getConfirmPassword() {
		return confirmPassword;
	}

	/**
	 * @return the selectedSupplierCompanyName
	 */
	public String getSelectedSupplierCompanyName() {
		return selectedSupplierCompanyName;
	}

	/**
	 * @return the selectedSupplierId
	 */
	public String getSelectedSupplierId() {
		return selectedSupplierId;
	}

	/**
	 * @return the supplierMasterService
	 */
	public SupplierMasterService getSupplierMasterService() {
		return supplierMasterService;
	}

	/**
	 * @return the userMasterPojo
	 */
	public UserMasterPojo getUserMasterPojo() {
		return userMasterPojo;
	}

	/**
	 * @return the userMasterService
	 */
	public UserMasterService getUserMasterService() {
		return userMasterService;
	}

	/**
	 * @param confirmPassword
	 *            the confirmPassword to set
	 */
	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	/**
	 * @param selectedSupplierCompanyName
	 *            the selectedSupplierCompanyName to set
	 */
	public void setSelectedSupplierCompanyName(
			String selectedSupplierCompanyName) {
		this.selectedSupplierCompanyName = selectedSupplierCompanyName;
	}

	/**
	 * @param selectedSupplierId
	 *            the selectedSupplierId to set
	 */
	public void setSelectedSupplierId(String selectedSupplierId) {
		this.selectedSupplierId = selectedSupplierId;
	}

	/**
	 * @param supplierMasterService
	 *            the supplierMasterService to set
	 */
	public void setSupplierMasterService(
			SupplierMasterService supplierMasterService) {
		this.supplierMasterService = supplierMasterService;
	}

	/**
	 * @param userMasterPojo
	 *            the userMasterPojo to set
	 */
	public void setUserMasterPojo(UserMasterPojo userMasterPojo) {
		this.userMasterPojo = userMasterPojo;
	}

	/**
	 * @param userMasterService
	 *            the userMasterService to set
	 */
	public void setUserMasterService(UserMasterService userMasterService) {
		this.userMasterService = userMasterService;
	}
}
